import os
import struct

DEVICE_PATH = "/dev/input/js0"  # Adjust based on your system

# Open the device file
with open(DEVICE_PATH, "rb") as f:
    while True:
        event = f.read(8)  # Read 8 bytes per event

        if event:
            time, value, event_type, number = struct.unpack("IhBB", event)
            #print(f"Time: {time}, Value: {value}, Type: {event_type}, Number: {number}")

            # Handle axis movement events (X, Y, Z, RZ)
            if event_type == 2:  # Axis movement event
                if number == 0:  # X axis
                    print(f"Time: {time}, X Axis Value: {value}")
                elif number == 1:  # Y axis
                    print(f"Time: {time}, Y Axis Value: {value}")
                elif number == 2:  # Z axis
                    print(f"Time: {time}, Z Axis Value: {value}")
                elif number == 3:  # RZ axis
                    print(f"Time: {time}, RZ Axis Value: {value}")

            # Handle button press/release events (Button A and Button B)
            elif event_type == 1:  # Button press/release event
                if number == 0:  # Button A
                    action = "pressed" if value == 1 else "released"
                    print(f"Time: {time}, Button A {action}")
                elif number == 1:  # Button B
                    action = "pressed" if value == 1 else "released"
                    print(f"Time: {time}, Button B {action}")

